package com.iwayin.core.bo.follow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iwayin.core.bo.BaseBO;
import com.iwayin.core.bo.EmailServiceUtil;
import com.iwayin.core.dao.fans.FansDAO;
import com.iwayin.core.dao.follow.FollowDAO;
import com.iwayin.core.dao.notification.NotificationDAO;
import com.iwayin.core.dao.setting.EmailSettingDAO;
import com.iwayin.core.dao.user.UserDAO;
import com.iwayin.core.dataobject.EmailSettingDO;
import com.iwayin.core.dataobject.FollowDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.exception.BOException;

public class FollowBOImpl extends BaseBO implements FollowBO {

	private FollowDAO followDAO;
	private FansDAO fansDAO;
	private UserDAO userDAO;
	private NotificationDAO notificationDAO;
	private EmailSettingDAO emailSettingDAO;

	@Override
	public void addFollow(FollowDO follow) {
		//检查有无关注过
		FollowDO f = followDAO.getFollower(follow.getUserId(), follow.getFollowId());
		if(f!=null)
			throw new BOException("关注失败:你已经关注了,去看看吧");
		//添加关注关系
		try {
			this.followDAO.addFollow(follow);
			this.fansDAO.addFans(follow);
			this.userDAO.followCountPlusOne(follow.getUserId());
			this.userDAO.fansCountPlusOne(follow.getFollowId());
			this.notificationDAO.addFansNotification(follow.getFollowId(), follow.getUserId());
			//是否立即接收邮件，今后将抽取出
			EmailSettingDO es = emailSettingDAO.getEmailSettingDO(follow.getFollowId());
			if(es != null && EmailSettingDO.USER_EMAIL_NOTIFICATION_IMMEDIATELY.equals(es.getFollowers())){
				UserDO fan = userDAO.getUserById(follow.getUserId());
				UserDO receiver = userDAO.getEmailReceiver(es.getUserId());
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("avatar", fan.getAvatarPath());
				param.put("nickname", fan.getNickname());
				param.put("summary", fan.getSummary());
				param.put("userId", fan.getId().toString());
				EmailServiceUtil.sendFollowEmail(receiver.getLoginName(), param);
			}
		} catch (Exception e) {
			loger.error("follow error", e);
			throw new BOException("关注失败:数据异常,请刷新后重试");
		}
	}

	@Override
	public void deleteFollow(FollowDO follow) {
		//检查有无关注
		FollowDO f = followDAO.getFollower(follow.getUserId(), follow.getFollowId());
		if(f==null)
			throw new BOException("抱歉,你还没有关注!感兴趣就关注一下吧");
		//取消操作
		try {
			this.followDAO.deleteFollow(follow);
			this.fansDAO.deleteFans(follow);
			this.userDAO.followCountMinusOne(follow.getUserId());
			this.userDAO.fansCountMinusOne(follow.getFollowId());
		} catch (Exception e) {
			loger.error("unfollow error", e);
			throw new BOException("取消关注失败:数据异常");
		}
	}

	@Override
	public List<UserDO> findFollowList(Long loginUserId, FollowDO query) {
		List<FollowDO> followList = followDAO.findFollowList(query);
		List<UserDO> userList = new ArrayList<UserDO>(followList.size());
		for (FollowDO f : followList) {
			UserDO user = userDAO.getUserById(f.getFollowId());
			userList.add(user);
			if (loginUserId == null) {
				continue;
			}
			if(user.getId().equals(loginUserId)){
				user.setMySelf(true);
				continue;
			}
			//判断与当前登录用户关系
			user.setFollow(checkFollow(loginUserId, f.getFollowId()));
		}
		return userList;
	}
	
	@Override
	public boolean checkFollow(Long userId, Long followId) {
		FollowDO follow = followDAO.getFollower(userId, followId);
		return follow==null?false:true;
	}


	@Override
	public List<UserDO> findFollowMostToday(Long size) {
		List<FollowDO> follows = this.followDAO.findFollowMostToday(size);
		List<UserDO> users = new ArrayList<UserDO>();
		if (follows != null&&follows.size()>0) {
			for (FollowDO follow : follows) {
				UserDO user = this.userDAO.getUserById(follow.getUserId());
				user.setFansCountToday(follow.getFollowCountToday());
				users.add(user);
			}
		}
		return users;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

	public void setFansDAO(FansDAO fansDAO) {
		this.fansDAO = fansDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setNotificationDAO(NotificationDAO notificationDAO) {
		this.notificationDAO = notificationDAO;
	}

	public void setEmailSettingDAO(EmailSettingDAO emailSettingDAO) {
		this.emailSettingDAO = emailSettingDAO;
	}
}
